package com.server.llh.llhserverwork.activity.food;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;

import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.activity.query.QueryUserCardInfoActivity;

import com.server.llh.llhserverwork.adapter.LLHHomeNewAdatpter;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.MyOrgInfoData;

import com.server.llh.llhserverwork.bean.ResonrceBean;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.utils.ToastTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 显示功能项导航
 */
public class LlhFoodServerTitleActivity extends BaseLeLaohuiAcitivity {
    @BindView(R.id.left_btn)
    ImageButton leftBtn;
    @BindView(R.id.titlebar_text)
    TextView titlebarText;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.function_menu)
    GridView function_menu;
    private String intentTag, intentTitle, orgId, token;
    MyBroadFinishActivity myBroadFinishActivity = null;
    private static final String TAG = "LlhFoodServer";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        orgId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID, getApplicationContext());
        token = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
        myBroadFinishActivity = new MyBroadFinishActivity();
        this.registerReceiver(myBroadFinishActivity, intentFilter);
        if (getIntent() != null) {
            intentTag = getIntent().getStringExtra("intentTag");
            intentTitle = getIntent().getStringExtra("intentTitle");
            titlebarText.setText(intentTitle);
            if ("1".equals(intentTag)) {
                function_menu.setAdapter(new LLHHomeNewAdatpter(getApplicationContext(), ResonrceBean.food_id, ResonrceBean.food_data));
            } else if ("0".equals(intentTag)) {
//                function_menu.setAdapter(new LLHHomeNewAdatpter(getApplicationContext(), ResonrceBean.server_id, ResonrceBean.server_data));
            } else {
//                function_menu.setAdapter(new LLHHomeNewAdatpter(getApplicationContext(), ResonrceBean.cammer_id, ResonrceBean.cammer_data));
            }
        }
        setGvOnItem();
    }

    private class MyBroadFinishActivity extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Constants.BroadCastAction.FINISH_ALL_ACTIVITY.equals(action)) {
                finish();
            }
        }
    }

    private void setGvOnItem() {
        function_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                if ("1".equals(intentTag)) {
                    switch (position) {
                        case 0:
                            intent = new Intent(getApplicationContext(), LlhFoodInfoActivity.class);
                            if (getIntent() != null) {
                                longtitude = getIntent().getStringExtra("longtitude");
                                latitude = getIntent().getStringExtra("latitude");
                                intent.putExtra("longtitude", longtitude);
                                intent.putExtra("latitude", latitude);
                                intent.putExtra("intentTag", "0");
                            }
                            break;
                        case 1:
//                            intent = new Intent(getApplicationContext(), LlhFoodOrderFragment.class);
                            intent = new Intent(getApplicationContext(), QueryUserCardInfoActivity.class);
                            intent.putExtra("intentTag", "2");
                            break;
                        case 2:
//                            intent = new Intent(getApplicationContext(), LlhAddressManagerActivity.class);
//                            intent.putExtra("intentTag", "0");
                            break;
                    }
                } else if ("0".equals(intentTag)) {
                    switch (position) {
                        case 0:
//                            intent = new Intent(getApplicationContext(), LlhServerActivityActivity.class);
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                    }
                } else {
                    switch (position) {
                        case 0:
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                    }
                }
                if (intent == null) {
                    T.showAnimToast(getApplicationContext(), "拼命开发中，敬请期待!");
                    return;
                } else {
                    startActivity(intent);
                }
            }
        });
    }

    String longtitude, latitude;

    @Override
    protected void onResume() {
        super.onResume();
        getOrgContent(null, null);
    }

    public static List<MyOrgInfoData.DataBean> myOrgInfoDataList = new ArrayList<>();
    MyOrgInfoData myOrgInfoData = null;
    MyOrgInfoData.DataBean myOrgData = null;

    public void getOrgContent(String longtitude, String latitude) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("token", token);
        lRequest.addParam("orgId", orgId);
        if (latitude != null && longtitude != null) {
            lRequest.addParam("latitude", latitude);
            lRequest.addParam("longitude", longtitude);
        }
        lRequest.setUri(ServerPort.GET_CANTINE_LIST);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), getResources().getString(R.string.toast_str));
            }

            @Override
            public void onNext(String responseBean, String code) {
                if ("0".equals(code) && responseBean != null) {
                    Log.d("responseBean", "responseBean==" + responseBean);
                    myOrgInfoDataList.clear();
                    try {
                        myOrgInfoData = new Gson().fromJson(responseBean, MyOrgInfoData.class);
                        if (myOrgInfoData != null) {
                            myOrgInfoDataList = myOrgInfoData.getData();
                        }
                    } catch (Exception e) {
                        myOrgData = new MyOrgInfoData.DataBean();
                        myOrgData.setOrgId("0");
                        myOrgData.setOrgName("搜索周边");
                        myOrgInfoDataList.add(myOrgData);
                        Log.e(TAG, "Exception==" + e.getMessage());
                    }

                } else {
                    ToastTool.showText(getApplicationContext(), "获取机构信息为空！");
                }
            }
        });
    }

    @Override
    public void showProgress() {
        try {
            showProgress(LlhFoodServerTitleActivity.this);
        } catch (Exception e) {

        }
    }

    @Override
    public void hideProgress() {
        try {
            closeProgress();
        } catch (Exception e) {

        }
    }

    @Override
    protected int getlayout() {
        return R.layout.llh_food_server_title;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    @Override
    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }

    @OnClick(R.id.left_btn)
    public void onViewClicked() {
        onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myBroadFinishActivity != null) {
            unregisterReceiver(myBroadFinishActivity);
        }
    }
}
